/*
 * @Description:
 * @Author: GaoQiao
 * @Date: 2024-05-09 17:12:20
 * @LastEditTime: 2025-11-11 16:10:13
 * @LastEditors: 873768511@qq.com
 */
import { MenuTypeEnum } from '@/types/systemManagement/menu';
import { Button, message, Popconfirm, Space } from 'antd';
import type { FC, ReactNode } from 'react';
import { useState } from 'react';
import { useModel } from 'umi';

interface ICustomEvent {
  hidden?: boolean;
  text?: ReactNode;
  menuType?: MenuTypeEnum;
  onClick?: () => void;
  preventEvent?: boolean;
  errorMsg?: string;
}

interface IProps {
  addEvent?: Omit<ICustomEvent, 'preventEvent' | 'errorMsg'>;
  editEvent?: Omit<ICustomEvent, 'preventEvent' | 'errorMsg'>;
  deleteEvent?: ICustomEvent;
}

const Operations: FC<IProps> = ({ addEvent, editEvent, deleteEvent }) => {
  const [popOpen, setPopOpen] = useState<boolean>(false);
  const { hasPermisson } = useModel('useUser', (model) => {
    return {
      hasPermisson: model.getRole,
    };
  });
  const editMenuType = editEvent?.menuType ?? false;
  const deleteMenuType = editEvent?.menuType ?? false;
  return (
    <Space>
      {addEvent?.hidden ? null : (
        <Button
          type="link"
          style={{ padding: 0 }}
          // disabled={
          //   !hasPermisson('MENU_CREATE_NEXT_CATALOG') ||
          //   (!hasPermisson('MENU_CREATE_CATALOG') && !hasPermisson('MENU_CREATE_MENU'))
          // }
          onClick={addEvent?.onClick}
        >
          {addEvent?.text ?? '新增下一级'}
        </Button>
      )}
      {editEvent?.hidden ? null : (
        <Button
          type="link"
          style={{ padding: 0 }}
          // disabled={
          //   editMenuType === MenuTypeEnum.CAT
          //     ? !hasPermisson('MENU_UPDATE_CATALOG')
          //     : editMenuType === MenuTypeEnum.MENU
          //       ? !hasPermisson('MENU_UPDATE_NENU')
          //       : false
          // }
          onClick={editEvent?.onClick}
        >
          {editEvent?.text ?? '编辑'}
        </Button>
      )}
      {deleteEvent?.hidden ? null : (
        <Popconfirm
          // disabled={
          //   deleteMenuType === MenuTypeEnum.CAT
          //     ? !hasPermisson('MENU_DELETE_CATALOG')
          //     : deleteMenuType === MenuTypeEnum.MENU
          //       ? !hasPermisson('MENU_DELETE_MENU')
          //       : false
          // }
          open={popOpen}
          title="是否删除"
          placement="topRight"
          onOpenChange={(open) => {
            if (open && deleteEvent?.preventEvent) {
              message.error(deleteEvent.errorMsg);
              return;
            }
            setPopOpen(open);
          }}
          onConfirm={deleteEvent?.onClick}
        >
          <Button
            type="link"
            // disabled={
            //   deleteMenuType === MenuTypeEnum.CAT
            //     ? !hasPermisson('MENU_DELETE_CATALOG')
            //     : deleteMenuType === MenuTypeEnum.MENU
            //       ? !hasPermisson('MENU_DELETE_MENU')
            //       : false
            // }
            style={{ padding: 0 }}
            danger
          >
            {deleteEvent?.text ?? '删除'}
          </Button>
        </Popconfirm>
      )}
    </Space>
  );
};

export default Operations;
